package controller;

import common.Homework;
import common.Topic;
import common.User;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.ResourceBundle;

public class CompleteListController implements Initializable {
    @FXML
    ListView<Homework> finished;
    @FXML
    ListView<User> unfinished;
    private ObservableList<Homework> finishedsource= FXCollections.observableArrayList();//数据源
    private ObservableList<User> unfinishedsource= FXCollections.observableArrayList();//数据源
    private Topic topic;
    public CompleteListController(Topic topic){
        for (HashMap.Entry< User, Homework> entry :topic.getHomeworks().entrySet()) {
            User stu = entry.getKey();
            Homework homework = entry.getValue();
            if(homework!=null){
                finishedsource.add(homework);
            }else{
                unfinishedsource.add(stu);
            }
        }
        this.topic=topic;
    }
    @Override
    public void initialize(URL location, ResourceBundle resources){
        finished.setItems(finishedsource);
        unfinished.setItems(unfinishedsource);
       finished.setCellFactory(new Callback<ListView<Homework>, ListCell<Homework>>() {
            @Override
            public ListCell<Homework> call(ListView<Homework> param) {
                return new ListCell<Homework>() {
                    @Override
                    protected void updateItem(Homework homework, boolean empty) {
                        super.updateItem(homework, empty);
                        if (empty || homework == null) {
                            setText(null);
                        } else {
                            setText(homework.getStudentname()+"-分数: "+homework.getScore());
                        }
                    }
                };
            }
        });
        unfinished.setCellFactory(new Callback<ListView<User>, ListCell<User>>() {
            @Override
            public ListCell<User> call(ListView<User> param) {
                return new ListCell<User>() {
                    @Override
                    protected void updateItem(User user, boolean empty) {
                        super.updateItem(user, empty);
                        if (empty || user == null) {
                            setText(null);
                        } else {
                            setText(user.getNumber()+" - "+user.getName());
                        }
                    }
                };
            }
        });
        finished.setOnMouseClicked(event -> {
            if(event.getClickCount()==2){
                // 获取双击时选中的项
                Homework selectedItem = finished.getSelectionModel().getSelectedItem();
                topic.setHomework(selectedItem);
                if(selectedItem==null) return;
                Stage stage=new Stage();
                FXMLLoader loader=null;
                DoHomeworkController doHomeworkController=new DoHomeworkController(topic,1);
                Pane root=null;
                loader=new FXMLLoader(getClass().getResource("/Interface/DoHomework.fxml"));
                loader.setController(doHomeworkController);
                try {
                    root=loader.load();
                } catch (IOException e) {
                    System.out.println("双击跳转作业界面失败");
                    e.printStackTrace();
                }
                Scene scene=new Scene(root);
                stage.setScene(scene);
                stage.getIcons().add(new Image("/Interface/Icon.png"));
                stage.setTitle(selectedItem.getStudentname());
                stage.setResizable(false);
                stage.show();
            }
        });
    }
}
